/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao;

import java.util.logging.Level;
import java.util.logging.Logger;
import negocio.GerenciarDB;

/**
 *
 * @author Rafaella
 */
public class JanelaDeletarUsuario extends javax.swing.JFrame {

    /**
     * Creates new form JanelaDeletarUsuario
     */
    public JanelaDeletarUsuario() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        DeletarUsuario = new javax.swing.JLabel();
        DeletarCPF = new javax.swing.JLabel();
        DelUsuario = new javax.swing.JFormattedTextField();
        ConfirmarDelUsuario = new javax.swing.JButton();
        CancelarDelUsuario = new javax.swing.JButton();
        todosCampos = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Deletar Usuário");
        setMinimumSize(new java.awt.Dimension(720, 460));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        DeletarUsuario.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        DeletarUsuario.setText("Deletar Usuário");
        getContentPane().add(DeletarUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(213, 33, -1, -1));

        DeletarCPF.setText("Digite o CPF do Usuário:");
        getContentPane().add(DeletarCPF, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 140, 130, 30));

        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("###.###.###-##");  
            DelUsuario = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        DelUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DelUsuarioActionPerformed(evt);
            }
        });
        getContentPane().add(DelUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 140, 150, 30));

        ConfirmarDelUsuario.setText("Confirmar");
        ConfirmarDelUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConfirmarDelUsuarioActionPerformed(evt);
            }
        });
        getContentPane().add(ConfirmarDelUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 260, 100, 30));

        CancelarDelUsuario.setText("Cancelar");
        CancelarDelUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarDelUsuarioActionPerformed(evt);
            }
        });
        getContentPane().add(CancelarDelUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 260, 100, 30));

        todosCampos.setForeground(new java.awt.Color(255, 0, 0));
        getContentPane().add(todosCampos, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 140, 200, 30));

        setBounds(0, 0, 728, 514);
    }// </editor-fold>//GEN-END:initComponents

    private void CancelarDelUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarDelUsuarioActionPerformed
        // TODO add your handling code here:
        this.dispose();
        JanelaInicioAdmin frame = new JanelaInicioAdmin();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }//GEN-LAST:event_CancelarDelUsuarioActionPerformed

    private void ConfirmarDelUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConfirmarDelUsuarioActionPerformed
        // TODO add your handling code here:
        String CPF = DelUsuario.getText().replaceAll("[.]","");
        CPF = CPF.replaceAll("[-]", "");
        
        if ((CPF.isEmpty()) || ("        ".equals(CPF)) ) {
            todosCampos.setText("O campo deve ser preenchido");
        } else {
            try {
                 GerenciarDB banco = new GerenciarDB();
                 
                 if (banco.checkUsuarioDB(CPF)) {
                    banco.delUsuario(CPF);
                    JanelaInicioAdmin frame = new JanelaInicioAdmin();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    this.dispose();
                 } else {
                     todosCampos.setText("Usuário incorreto ou inexistente.");
                 }
            } catch (Exception ex) {
                Logger.getLogger(JanelaDeletarCartao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }//GEN-LAST:event_ConfirmarDelUsuarioActionPerformed

    private void DelUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DelUsuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DelUsuarioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaDeletarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaDeletarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaDeletarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaDeletarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JanelaDeletarUsuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelarDelUsuario;
    private javax.swing.JButton ConfirmarDelUsuario;
    private javax.swing.JFormattedTextField DelUsuario;
    private javax.swing.JLabel DeletarCPF;
    private javax.swing.JLabel DeletarUsuario;
    private javax.swing.JLabel todosCampos;
    // End of variables declaration//GEN-END:variables
}
